﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.Application.hqApplication.Commands.Ablactationss;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Eliminate
{
    public class AddEliminateHander : IRequestHandler<AddEliminateCommand, Result>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Eliminates> eliminatesrepository;

        public AddEliminateHander(IMapper mapper, IBaseRepository<Eliminates> eliminatesrepository)
        {
            this.mapper = mapper;
            this.eliminatesrepository = eliminatesrepository;
        }

        public async Task<Result> Handle(AddEliminateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Eliminates entity = mapper.Map<Eliminates>(request);
            var res = await eliminatesrepository.Add(entity);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
            }
            result.Msg = "添加成功";
            return result;
        }
    }
}
